package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessMyFarm;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyFarmListVO;
import com.digitalchina.lxnsy.interfaces.IDeleteBack;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.MyFarmListHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/12/14 11:01
 * 修改备注：
 * ================================================
 */
public class MyFarmListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, IDeleteBack {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;

    private List<MyFarmListVO> voList = new ArrayList<>();

    int delPos = -1;


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_my_farm_list);
        setTitle("我的农场");
        setRightText("添加");
    }

    @Override
    public void initMember() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyFarmListHolder(parent, MyFarmListActivity.this);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (isFinishing())
                    return;
                switch (msg.what) {
                    case MSG.MSG_FARM_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_FARM_LIST_FIELD:
                        showToast(msg.obj.toString());
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_DELETE_FARM_SUCCESS:
                        showToast("删除成功");
                        adapter.remove(delPos);
                        break;
                    case MSG.MSG_DELETE_FARM_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), MyFarmListVO.class);

        adapter.clear();

        if (voList != null)
            adapter.addAll(voList);
    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoForResultActivity(MyFarmListActivity.this, AddMyFarmActivity.class, 850);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        BusinessMyFarm.farmList(this, mHandler);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 850 && resultCode == RESULT_OK)
            onRefresh();
    }

    @Override
    public void clickOk(int pos) {
        delPos = pos;
        MyFarmListVO vo = (MyFarmListVO) adapter.getAllData().get(pos);
        BusinessMyFarm.deleteFarm(MyFarmListActivity.this, vo.getId(), mHandler);
    }
}
